Skip to content

Conversation

@tim-one
Copy link
Member

@tim-one tim-one commented Jan 17, 2026

gh-143948: Explain graphlib's cycle-finding code

Long overdue comments explaining what's going on, and why. No code changes, just comments.

@tim-one tim-one self-assigned this Jan 17, 2026
@tim-one tim-one added skip news stdlib Standard Library Python modules in the Lib/ directory labels Jan 17, 2026
@tim-one tim-one linked an issue Jan 17, 2026 that may be closed by this pull request
@tim-one tim-one changed the title Explain graphlib's cycle-finding code GH-143948: Explain graphlib's cycle-finding code Jan 17, 2026
Comment on lines +258 to +259

# On Finding Cycles
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# On Finding Cycles
# On Finding Cycles

# There is a (at least one) total order if and only if the graph is
# acyclic.
#
# When it is cyclic, "there's a cycle - somewhere!" isn't very helpful.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would argue that knowing that there is a cycle could still be useful! so

Suggested change
# When it is cyclic, "there's a cycle - somewhere!" isn't very helpful.
# When it is cyclic, "there's a cycle - somewhere!" is not always helpful.

#
# When it is cyclic, "there's a cycle - somewhere!" isn't very helpful.
# In theory, it would be most helpful to partition the graph into
# strongly connected components (SCCs) and display those with more than
Copy link
Member

@picnixz picnixz Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Others could argue that WCCs are also enough for their cases. Do you want to explain the difference?

# That's a lot of work, though, and we can get most of the benefit much
# more easily just by showing a single specific cycle.
#
# Finding a cycle is most natural via a breadth first search, which can
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Finding a cycle is most natural via a breadth first search, which can
# Finding a cycle is most natural via a breadth-first search (BFS), which can

I am not entirely sure for the dash. I would however mention BFS because readers may be familiar with the abbreviation (I am personally more used to the abbrev than the full word... but this is what happens when you use it a lot).

Comment on lines +306 to +307
# Worst case time is linear in the number of nodes plus the number of
# edges. Worst case memory burden is linear in the number of nodes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Worst case time is linear in the number of nodes plus the number of
# edges. Worst case memory burden is linear in the number of nodes.
# If |V| is the number of vertices and |E| is the number of edges,
# worst case time is O(|V| + |E|) and worst case memory burden is O(|V|).

Strictly speaking this is just the usual complexity of DFS. But I think we can use the same notations as Wikipedia here. Unless you explicitly want to avoid O-notation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review skip news stdlib Standard Library Python modules in the Lib/ directory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Explain graphlib's cycle-finding code

2 participants